package cn.quevo.core.entity;

import cn.quevo.core.enums.EnumsInterface;
import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CustomException;
import cn.quevo.core.lang.NumberUtils;
import cn.quevo.core.lang.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回结果
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 14:55:10
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
@Data
@NoArgsConstructor
public class ResultEntity implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -874039914230807080L;
    /**
     * 返回信息编码[200:成功,非200失败]
     */
    private Integer status;
    /**
     * 错误信息提示
     */
    private String msg;

    public ResultEntity(EnumsInterface enums) {
        this.status = NumberUtils.toInt(StringUtils.valueOf(enums.getCode()), 50000);
        this.msg = enums.getValue();
    }

    public ResultEntity(CustomException exception) {
        this.status = exception.getStatus();
        this.msg = exception.getMsg();
    }

    public ResultEntity(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public static ResultEntity success() {
        return new ResultEntity(ResultStatusEnum.SUCCESS);
    }

    @Override
    public String toString() {
        return StringUtils.isBlank(status) ? this.msg : "[".concat(this.status + "").concat("]").concat(this.msg);
    }
}